﻿using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Spring.Context.Support;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Application.MainBoundedContext.SystemModule
{
    public class ReceiptCodeRuleAppService : BaseAppService<ReceiptCodeRule, ReceiptCodeRuleSearch, IReceiptCodeRuleRepository>, IReceiptCodeRuleAppService
    {
        private IEmployeeAppService EmployeeAppService { get; set; }
        private ICodeRulePropertyAppService CodeRulePropertyAppService { get; set; }

        #region IAccountAppService Members

        public override BoolGenericResult<ReceiptCodeRule> AddOrUpdate(ReceiptCodeRule entity)
        {
            this.SpringContext = ContextRegistry.GetContext();
            BoolGenericResult<ReceiptCodeRule> result = new BoolGenericResult<ReceiptCodeRule>();
            if (entity.IsNull())
            {
                throw new ArgumentNullException("");
            }
            this.EmployeeAppService = this.SpringContext.GetObject(SystemModuleConst.EMPLOYEEAPPSERVICE) as IEmployeeAppService;
            this.CodeRulePropertyAppService = this.SpringContext.GetObject(SystemModuleConst.CODERULEPROPERTYAPPSERVICE) as ICodeRulePropertyAppService;
            Employee employee = this.EmployeeAppService.GetBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id)).FirstOrDefault();
            Administrator admin = new Administrator(employee);

            ReceiptCodeRule oldReceiptCodeRule = this.Repository.Get(entity.Id);
            CodeRuleProperty codeRuleProperty = null;
            if (oldReceiptCodeRule.IsNull())
            {
                result = admin.AddReceiptCodeRule(entity.Name, entity.Type, entity.Prefix, entity.IsDefault, entity.SerialNumberNumber, entity.SerialNumberStart, entity.Pattern,
                    entity.YearNumber,entity.Example);
                if (result.IsSuccess)
                {
                    base.Add(result.Object);
                    codeRuleProperty = ((EditReceiptCodeRule)result).CodeRuleProperty;
                    codeRuleProperty.CodeRuleID = result.Object.Id;
                    this.CodeRulePropertyAppService.Add(((EditReceiptCodeRule)result).CodeRuleProperty);
                }
            }
            else
            {
                CodeRulePropertySearch codeRulePropertySearch = SearchFactory.CreateCodeRulePropertySearch(oldReceiptCodeRule.Id, SystemIdentifier.CodeRuleType.Receipt.GetHashCode());
                codeRuleProperty = this.CodeRulePropertyAppService.GetSingleBySearch(codeRulePropertySearch);
                result = admin.UpdateReceiptCodeRule(oldReceiptCodeRule, codeRuleProperty, entity.Name, entity.Type, entity.Prefix, entity.IsDefault, entity.SerialNumberNumber, entity.SerialNumberStart,
                    entity.Pattern, entity.YearNumber, entity.Example);
                if (result.IsSuccess)
                {
                    base.Update(result.Object);
                    this.CodeRulePropertyAppService.Update(codeRuleProperty);
                }
            }
            return result;
        }

        #endregion
    }
}
